#include "twittoz.h"
#include "ui_twittoz.h"
 #include <QDesktopWidget>
#include <QtGlobal>
#include <QtDebug>
#include <iostream>
#include <QFile>
#include <QMessageBox>

Twittoz::Twittoz(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::Twittoz)
{
    m_ui->setupUi(this);
    QImage image_fond("../images/fond.jpg");
    QImage image_fondBlanc("../images/fondblanc.png");
    QImage image_logo("../images/logo.png");
    QImage image_oiseau("../images/ptioiseau.png");
    QImage image_oiseau2("../images/oiseau2.png");
    QImage image_abonnement("../images/abonnement.png");
    QImage image_abonne("../images/abonne.jpg");

    m_ui->fond->setPixmap(QPixmap::fromImage(image_fond));
    m_ui->fondBlanc->setPixmap(QPixmap::fromImage(image_fondBlanc));
    m_ui->logo->setPixmap(QPixmap::fromImage(image_logo));
    m_ui->oiseau->setPixmap(QPixmap::fromImage(image_oiseau));
    m_ui->oiseau2->setPixmap(QPixmap::fromImage(image_oiseau2));
    m_ui->abonnement->setPixmap(QPixmap::fromImage(image_abonnement));
    m_ui->abonne->setPixmap(QPixmap::fromImage(image_abonne));

    _api = NULL;

QDesktopWidget bureau;
QRect surface_bureau = bureau.screenGeometry();
int x = surface_bureau.width()/2 - width()/2;
int y = (int)((double)surface_bureau.height()/2.5) - height()/2;
this->move(x,y);
}

Twittoz::~Twittoz()
{
    delete m_ui;
}

void Twittoz::on_textEdit_textChanged()
{
    qint32  size = m_ui->textEdit->toPlainText().size();

    m_ui->progressBar->setValue(size);
    if (size < 140)
        m_ui->Send->setEnabled(true);
    else
        m_ui->Send->setEnabled(false);
}

void Twittoz::on_Send_clicked()
{
    QString message;
    int error = 0;
    if (this->_api)
    {
        this->_api->updateStatus(m_ui->textEdit->toPlainText().toStdString());
        m_ui->textEdit->clear();
        message = "Your status has been successfully updated.";
    }
    else
    {
        message = "You idiot, you must be logged in to do that !";
        error = 1;
    }
    QMessageBox toz(this);
    toz.setText(message);
    toz.exec();
    if (error)
        exit(-1);
    this->fillTweets();
}

void Twittoz::loadAuth()
{
    QFile file("../.auth.tw");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;
    QString fileCreds = file.readLine();
    std::string creds = fileCreds.toStdString();
    this->_user = fileCreds.split(':')[0];
    this->_pass = fileCreds.split(':')[1];
    m_ui->userName->setText(this->_user);
    this->_api = new TwitterAPI(creds);
    m_ui->abonneNum->setText(QString().setNum(JsonParser::extract_list(this->_api->getFollowers(this->_user.toStdString())).size()));
    m_ui->abonnementNum->setText(QString().setNum(JsonParser::extract_list(this->_api->getFollowing(this->_user.toStdString())).size()));
    file.close();
    this->fillTweets();
}

void Twittoz::fillTweets()
{
    std::list<std::string> result;
    std::map<std::string, std::string> extract;
    std::map<std::string, std::string> user;
    std::list<std::string>::iterator it;
    std::list<std::string>::iterator ite;

    QLayoutItem *child;
    QLayoutItem *childLayout;
    while ((child = m_ui->tweetsLayout->takeAt(0)) != 0) {
        while ((childLayout = child->layout()->takeAt(0)) != 0)
        {
            childLayout->widget()->hide();
            delete childLayout;
        }
        delete child;
    }
    result = JsonParser::extract_list(this->_api->homeTimeline());
    it = result.begin();
    ite = result.end();
   for (int i = 0; it != ite ; i++,it++)
    {
        extract = JsonParser::extract_map(*it);
        user = JsonParser::extract_map(extract["user"]);

        QVBoxLayout *newLayout = new QVBoxLayout;
        newLayout->setSpacing(1);
        newLayout->setSizeConstraint(newLayout->SetNoConstraint);
        QLabel *label = new QLabel(NULL);
        label->setText(QString(user["screen_name"].c_str()) + " :");
        QFont fontLabel;
        fontLabel.setFamily(QString::fromUtf8("Lucida Handwriting"));
        fontLabel.setPointSize(11);
        fontLabel.setBold(true);
        fontLabel.setWeight(30);
        label->setFont(fontLabel);
        newLayout->insertWidget(0, label, 0, Qt::AlignTop);

         QLabel *date = new QLabel(NULL);
        date->setText(QString(user["created_at"].c_str()).split("+")[0]);
        fontLabel.setPointSize(6);
        fontLabel.setBold(false);
        date->setFont(fontLabel);
        newLayout->insertWidget(1, date, 0, Qt::AlignTop);

        QTextEdit *text = new QTextEdit(NULL);
        QFont fontText;
        fontText.setFamily(QString::fromUtf8("Arial"));
        fontText.setPointSize(9);
        fontText.setBold(true);
        fontText.setWeight(30);
        text->setFont(fontText);
        text->setText(QString(extract["text"].c_str()));
        text->setReadOnly(true);
        text->setMaximumHeight(30);
        newLayout->insertWidget(2, text, 0, Qt::AlignTop);
        m_ui->tweetsLayout->insertLayout(i, newLayout, 0);
        if (i == 5)
            break;
    }
}

void Twittoz::on_refresh_clicked()
{
    if (this->_api != NULL)
        this->fillTweets();
    else
    {
        QMessageBox toz(this);
        toz.setText("You must be logged in ! Bye bye.");
        toz.exec();
        exit(-1);
    }
}

void Twittoz::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
